﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SupplyChain.ErrorCode
{
    public class PagedResponse<T>
    {
        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageNumber { get; set; }

        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRecords { get; set; }

        /// <summary>
        /// 数据
        /// </summary>
        public IEnumerable<T> Data { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public PagedResponse(int pageNumber, int pageSize, int totalRecords, IEnumerable<T> data)
        {
            PageNumber = pageNumber;
            PageSize = pageSize;
            TotalRecords = totalRecords;
            TotalPages = (int)Math.Ceiling(totalRecords / (double)pageSize);
            Data = data;
        }

        /// <summary>
        /// 创建成功的分页响应
        /// </summary>
        public static ApiResponse<PagedResponse<T>> Success(int pageNumber, int pageSize, int totalRecords, IEnumerable<T> data, string message = "Success")
        {
            var pagedResponse = new PagedResponse<T>(pageNumber, pageSize, totalRecords, data);
            return ApiResponse<PagedResponse<T>>.Success(pagedResponse, message);
        }

        /// <summary>
        /// 创建错误的分页响应
        /// </summary>
        public static ApiResponse<PagedResponse<T>> Error(int statusCode, string message)
        {
            return ApiResponse<PagedResponse<T>>.Error(statusCode, message);
        }
    }
}
